import enum
from datetime import datetime

from sqlalchemy.sql.expression import false

from .database import db

class Role(enum.Enum):
    admin = 'admin'
    student = 'student'
    professor = 'professor'

class Gender(enum.Enum):
    Male = 'Male'
    Female = 'Female'

class CourseState(enum.Enum):
    not_started = 'not_started'
    signing_in = 'signing_in'
    started = 'started'
    finished = 'finished'

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.now, nullable=False, onupdate=datetime.now)
    role = db.Column(db.Enum(Role), nullable=False)
    name = db.Column(db.String(32), nullable=False)
    email = db.Column(db.String(64), nullable=False, unique=True, index=True)
    hashed_password = db.Column(db.String(128), nullable=False)
    avatar = db.Column(db.String(64), nullable=False)
    age = db.Column(db.Integer)
    gender = db.Column(db.Enum(Gender))
    address = db.Column(db.String(128))
    post = db.Column(db.String(128))
    skill_points = db.Column(db.JSON)
    key_ompetency = db.Column(db.String(128))

class Course(db.Model):
    __tablename__ = 'courses'

    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.now, nullable=False, onupdate=datetime.now)
    name = db.Column(db.String(128), nullable=False)
    about = db.Column(db.String(2048))
    online = db.Column(db.Boolean, default=false, nullable=False)
    school_year = db.Column(db.Integer, nullable=False)
    trimester = db.Column(db.Integer, nullable=False)
    professor_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    duration = db.Column(db.Integer, nullable=False)
    lessons = db.Column(db.Integer, nullable=False)
    progress = db.Column(db.Integer, nullable=False)
    number_of_students = db.Column(db.Integer, default=0, nullable=False)

    professor = db.relationship('User')
    timetables = db.relationship('Timetable', back_populates='course')

class Timetable(db.Model):
    __tablename__ = 'timetables'

    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.now, nullable=False, onupdate=datetime.now)
    course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False)
    title = db.Column(db.String(128), nullable=False)
    about = db.Column(db.String(2048))
    open_time = db.Column(db.DateTime)
    start_time = db.Column(db.DateTime, nullable=False)
    end_time = db.Column(db.DateTime, nullable=False)
    place = db.Column(db.String(255), nullable=False)
    state = db.Column(db.Enum(CourseState), nullable=False)
    number_of_sign_in = db.Column(db.Integer, default=0, nullable=False)

    course = db.relationship('Course', back_populates='timetables')

class Attendance(db.Model):
    __tablename__ = 'attendance'

    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.now, nullable=False, onupdate=datetime.now)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    timetable_id = db.Column(db.Integer, db.ForeignKey('timetables.id'), nullable=False)

    __table_args__ = (
        db.Index('ix_attendance_user_id_timetable_id', user_id, timetable_id, unique=True),
    )

class Participant(db.Model):
    __tablename__ = 'participants'

    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
    updated_at = db.Column(db.DateTime, default=datetime.now, nullable=False, onupdate=datetime.now)
    user_id  = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    course_id = db.Column(db.Integer, db.ForeignKey('courses.id'), nullable=False)

    __table_args__ = (
        db.Index('ix_participants_user_id_course_id', user_id, course_id, unique=True),
    )
